package rmy;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.rmy.common.core.text.Convert;
import rmy.domain.BerrArea;
import rmy.domain.BerrParam;
import rmy.service.impl.OnbonBerrDevice;
import rmy.service.impl.OnbonBx06BerrDevice;

import java.util.ArrayList;
import java.util.List;

public class BerrUtils {


    /**
     * 通过参数获取驱动类型 1 仰邦
     * @param strParams
     * @return
     */
    public String getDriverType(String strParams) {
        JSONObject jsonObject = JSONObject.parse(strParams);
        return Convert.toStr(jsonObject.get("driver_type"));
    }

    /**
     * 解析成对象
     * @param strContent
     * @return
     */
    public static List<BerrArea> parseContent(String strContent){
        List<BerrArea> compAreaDynamicList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parse(strContent);
        JSONArray jsonArray = jsonObject.getJSONArray("areas");
        for (int i = 0;  i < jsonArray.size(); i++) {
            BerrArea compAreaDynamic = JSONObject.parseObject(jsonArray.getJSONObject(i).toString(), BerrArea.class);
            compAreaDynamicList.add(compAreaDynamic);
        }
        return compAreaDynamicList;
    }

    /**
     * 解析成对象
     * @param strParams
     * @return
     */
    public static BerrParam parseParams(String strParams){
        BerrParam berrParam = JSONObject.parseObject(strParams, BerrParam.class);
        return berrParam;
    }

    public static boolean sendBerr(String strIp, int nPort, String strParams, List<BerrArea> compAreaList) throws Exception {
        BerrParam berrParam = parseParams(strParams);
        // 1 仰邦
        switch (Convert.toStr(berrParam.driver_type, "1"))
        {
            case "2":
                new OnbonBerrDevice().sendBerr(strIp, nPort, berrParam, compAreaList);
                break;
            default:
                new OnbonBx06BerrDevice().sendBerr(strIp, nPort, berrParam, compAreaList);
                break;
        }
        return true;
    }

}
